package com.dianrui.operation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.bean.Menu;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainMenuAdapter extends BaseAdapter {

    Context context;
    List<Menu> list;

    public MainMenuAdapter(Context context, List<Menu> list) {
        this.context = context;
        this.list = list;
    }

    public void refresh(List<Menu> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.mainmenu_grid_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (list.size() > 0) {
            Menu menu = list.get(position);
            holder.menuName.setText(menu.menu_name);
            holder.menuAction.setText(menu.menu_action);
            switch (menu.menu_id){
                case "212":
                    holder.mainImg.setImageResource(R.mipmap.main_212);
                    break;
                case "222":
                   holder.mainImg.setImageResource(R.mipmap.main_212);
                    break;

                case "213":
                    holder.mainImg.setImageResource(R.mipmap.main_213);
                    break;

                case "214":
                    holder.mainImg.setImageResource(R.mipmap.main_214);
                    break;

                case "215":
                    holder.mainImg.setImageResource(R.mipmap.main_215);
                    break;

                case "216":
                    holder.mainImg.setImageResource(R.mipmap.main_216);
                    break;

                case "217":
                    holder.mainImg.setImageResource(R.mipmap.main_217);
                    break;


                case "218":
                    holder.mainImg.setImageResource(R.mipmap.main_218);
                    break;

                case "219":
                    holder.mainImg.setImageResource(R.mipmap.main_220);
                    break;

                case "220":
                    holder.mainImg.setImageResource(R.mipmap.main_219);
                    break;

                case "221":
                    holder.mainImg.setImageResource(R.mipmap.main_221);
                    break;




                default:
                        break;
            }
        }
        return convertView;
    }

     class ViewHolder {
         @BindView(R.id.main_img)
         ImageView mainImg;
        @BindView(R.id.menu_name)
        TextView menuName;
         @BindView(R.id.menu_action)
         TextView menuAction;
        @BindView(R.id.menu_layout)
        LinearLayout menuLayout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
